package com.orangelife.mobile.common.activity;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import com.common.jump.CommonJumper;
import com.common.sp.UserSp;
import com.curry.android.net.NetworkUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.StringUtil;
import com.datatrack.DataTrack;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.common.fragment.OrangeLifeBaseFragment;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

import java.util.Map;

import cn.jpush.android.api.JPushInterface;

public class OrangeLifeBaseActivity extends FragmentActivity {
    protected Dialog dialog = null;
    private boolean isDestory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CurryApplication.getInstance().addActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initJPush();
        initUmeng();
        dataTrackBrowse();
    }

    public Map<String, Object> getDataTrack() {
        return null;
    }

    /**
     * 诸葛全局浏览采集，在将来要改成抽象方法
     */
    private void dataTrackBrowse() {
        DataTrack.browseTrack(getDataTrack());
    }

    public void dataTrackClick(String ui_elmt, Map<String, Object> map) {
        DataTrack.clickTrack(ui_elmt, map);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }

    private void initJPush() {
        JPushInterface.onResume(this);
    }

    private void initUmeng() {
        MobclickAgent.onResume(this);
        //umeng统计应用启动数据
        PushAgent.getInstance(this).onAppStart();
    }

    protected void setTitleText(TextView tv, String text) {
        tv.setText(text);
    }

    public void closeActivity() {
        if (StringUtil.isContainBlank(UserSp.getLoginID())) {
            finish();
        }
    }

    /**
     * 是否联网
     *
     * @param isToast 是否弹出Toast提示
     * @return true-联网，false-断网；
     */
    protected boolean isConnected(boolean isToast) {
        if (!NetworkUtil.isConnectedNet(this)) {
            if (isToast) {
                Toast.makeText(this,
                        getResources().getString(com.curry.android.R.string.net_disconnected),
                        Toast.LENGTH_SHORT).show();
            }
            return false;
        }
        return true;
    }

    protected boolean isConnected() {
        return isConnected(true);
    }

    /** XML中属性onClick用 */
    public void onBackClick(View v) {
        finish();
        /*增加动画*/
        IntentHelper.getInstances().jumpActivityAnimation(this, true);
        dataTrackClick("btnBack", null);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        /*增加动画*/
        DataTrack.clickTrack("btnBack", null);
        IntentHelper.getInstances().jumpActivityAnimation(this, true);
    }

    /** 如果是Activity生命周期内调用此方法，则不需要判断Activity是否已被销毁或者为null，这是不可能发生的事情 */
    public void showDialog() {
        if (dialog != null && dialog.isShowing()) {/*避免同时开两个dialog，造成第二个dialog无法关闭*/
            dialog.dismiss();
        }
        dialog = DialogHelper.getInstance().createLoadingDialog(
                this);
        dialog.show();
    }

    public void showDialog(Activity activity) {
        if (activity != null && !isDestory) {
            dialog = DialogHelper.getInstance().createLoadingDialog(
                    activity);
            dialog.show();
        }
    }

    public void closeDialog() {
        if (!isDestory && dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    /**
     * 需要重新登录app
     */
    protected void isLogin(Dialog dialog) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        CommonJumper.jumpToLoginWhenExitWithClear(this);
    }

    protected void isLogin(Dialog dialog, Activity ac) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        CommonJumper.jumpToLoginWhenExitWithClear(this);
    }

    long lastClickTime = 0;

    public boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (timeD >= 0 && timeD <= 500) {
            return true;
        } else {
            lastClickTime = time;
            return false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isDestory = true;
        closeDialog();
        closeKeybord();
        CurryApplication.getInstance().finishActivity(this);
    }

    private void closeKeybord() {
        if (getCurrentFocus() != null) {
            ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(getCurrentFocus()
                                    .getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 获取总积分
     *
     * @return
     */
    protected String getTotalCredit() {
        return UserSp.getPoint();
    }

    /** 当前展示的Fragment */
    protected Fragment currentFragment;

    /**
     * 用Fragment替换视图
     *
     * @param resView        将要被替换掉的视图
     * @param targetFragment 用来替换的Fragment
     */
    public void changeFragment(int resView, OrangeLifeBaseFragment targetFragment, Bundle bundle) {
        if (targetFragment == null || targetFragment.equals(currentFragment)) {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        if (!targetFragment.isAdded()) {
            transaction.add(resView, targetFragment, targetFragment.getClass()
                    .getName());
        }
        if (targetFragment.isHidden()) {
            transaction.show(targetFragment);
//            targetFragment.onChange();
        }
        if (currentFragment != null && currentFragment.isVisible()) {
            transaction.hide(currentFragment);
        }
        currentFragment = targetFragment;
        if (bundle != null) {
            currentFragment.setArguments(bundle);
        }
        transaction.commit();
    }

    public void changeFragment(int resView, OrangeLifeBaseFragment targetFragment) {
        changeFragment(resView, targetFragment, null);
    }
}
